// packageK/index/index.js
const app = getApp()
// const host = require('/host.js');
import newHost from '../../host';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [],
    newGoods: null,
    recommendGoodsList: [],
    platform: '',
    brandsList: [
      {id: 37, name: '大疆', type: 'brand', logo: '/static/images/大疆.jpg'},
      {id: 38, name: '帝瓦雷', type: 'brand', logo: '/static/images/帝瓦雷.jpg'},
      {id: 31, name: '坚果', type: 'brand', logo: '/static/images/坚果.jpg'},
      {id: 9, name: '洁碧', type: 'brand', logo: '/static/images/洁碧.jpg'},
      {id: 10, name: '科沃斯', type: 'brand', logo: '/static/images/科沃斯.jpg'},
      {id: 27, name: '乐高', type: 'brand', logo: '/static/images/乐高.jpg'},
      {id: 12, name: '马歇尔', type: 'brand', logo: '/static/images/马歇尔.jpg'},
      {id: 39, name: '美的', type: 'brand', logo: '/static/images/美的.jpg'},
      {id: 23, name: '摩飞', type: 'brand', logo: '/static/images/摩飞.jpg'},
      {id: 14, name: '韶音', type: 'brand', logo: '/static/images/韶音.jpg'},
      {id: 21, name: '松下', type: 'brand', logo: '/static/images/松下.jpg'},
      {id: 33, name: 'jbl', type: 'brand', logo: '/static/images/jbl.jpg'}
    ],
    priceRanges: [],
    host: ''
  },

  /**
   * 扫码
   */
  scanCode: function() {
    wx.scanCode({
      success: function(res) {
        if (res.result) {
          wx.navigateTo({
            url: '/packageC/member/searchAll/searchAll?keyword=' + res.result
          })
        }
      },
      fail: function(err) {
        console.log(err)
      }
    })
  },

  /**
   * 搜索框获取焦点跳转到搜索页面
   */
  toSearch: function() {
    wx.navigateTo({
      url: '/packageI/searchPage/searchPage'
    })
  },

  /**
   * 获取页面数据
   */
  getPageData: function() {
    const urlStr = app.getNetAddresss('home.index');
    const that = this;
    app._getNetWork({
      url: urlStr,
      success: function(res) {
        const { result, data, msg } = res.data
        if (result == 1) {
          const {
            banner,
            new_goods,
            recommend_goods_list,
            brands,
            priceRanges
          } = data
          // 轮播图
          if (banner && banner.length) {
            that.setData({ swiperList: banner })
          }
          // 新品上架商品
          if (new_goods) {
            that.setData({ newGoods: new_goods })
          }
          // 热门推荐商品
          if (recommend_goods_list && recommend_goods_list.length) {
            that.setData({ recommendGoodsList: recommend_goods_list })
          }
          // 品牌专区
          // if (brands && brands.length) {
          //   that.setData({ brandsList: brands})
          // }
          // 礼品专区
          if (priceRanges && priceRanges.length) {
            that.setData({ priceRanges })
          }
        } else {
          app.tips(msg || '获取数据失败')
        }
      }
    })
  },

  /**
   * 跳转到商品详情
   * @param {Object} e 
   */
  gotoGoodsDetail: function(e) {
    const { id } = e.currentTarget.dataset
    if (!id) {
      return
    }
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + id,
    })
  },

  /**
   * 品牌专区跳转
   * @param {Object} e 
   */
  goBrands: function(e) {
    const { item } = e.currentTarget.dataset
    let url = ''
    if (item.type == 'brand') {
      url = '/packageB/member/category/brandgoods/brandgoods?id='
    } else if (item.type == 'category') {
      url = '/packageB/member/category/search_v2/search_v2?id='
    }
    if (url) {
      wx.navigateTo({
        // url: '/packageB/member/category/brandgoods/brandgoods?id=' + item.id,
        url: url + item.id
      })
    }
  },

  /**
   * 礼品专区跳转
   * @param {Object} e 
   */
  goSearchGoods: function(e) {
    const { item } = e.currentTarget.dataset
    if (item) {
      const {min_price, max_price} = item
      wx.navigateTo({
        url: `/packageB/member/category/search_v2/search_v2?min_price=${min_price}&max_price=${max_price}`,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('host:::', newHost.host)
    this.getPageData()
    const deviceInfo = wx.getDeviceInfo()
    const { platform } = deviceInfo
    this.setData({ platform, host: newHost.host })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      imageUrl: '/image/share.jpg'
    }
  }
})